package com.qdhhxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qdhhxy.entity.StuEntity;
import com.qdhhxy.service.IStuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/stu")
public class StuController {

    @Autowired
    private IStuService stuService;

    // 分页查询接口
    @GetMapping("/list")
    public IPage<StuEntity> getStuList(@RequestParam int pageNum, @RequestParam int pageSize) {
        return stuService.getStuPage(pageNum, pageSize);
    }

    // 添加学生接口
    @PostMapping("/add")
    public String addStu( StuEntity stuEntity) {
        boolean result = stuService.save(stuEntity);
        return result ? "添加成功" : "添加失败";
    }
    // 1. 基本查询示例
    @GetMapping("/select")
    public ResponseEntity<List<StuEntity>> basicQuery() {
        // 创建查询条件
        QueryWrapper<StuEntity> wrapper = new QueryWrapper<>();
        // 选择要查询的字段
        wrapper.select("bh","xm","dh")
                // 添加等于条件
                .eq("bh", "a01")
//                 按创建日期降序排序
                .orderByAsc("dh");
        // 执行查询
        return ResponseEntity.ok(stuService.list(wrapper));
    }

    // 2. Lambda查询示例
    @GetMapping("/lambda")
    public ResponseEntity<List<StuEntity>> lambdaQuery() {
        // 创建Lambda查询条件
        LambdaQueryWrapper<StuEntity> wrapper = new LambdaQueryWrapper<>();
        // 选择要查询的字段
        wrapper.select(StuEntity::getBh, StuEntity::getXm, StuEntity::getDh)
                // 添加等于条件
                .eq(StuEntity::getXm, "小李");
        // 执行查询
        return ResponseEntity.ok(stuService.list(wrapper));
    }
    // 3. 模糊查询示例
    @GetMapping("/like")
    public ResponseEntity<List<StuEntity>> likeQuery(@RequestParam String keyword) {
        QueryWrapper<StuEntity> wrapper = new QueryWrapper<>();
        wrapper.like("bh", keyword)
                .or()
                .like("xm", keyword);
        return ResponseEntity.ok(stuService.list(wrapper));
    }


}